package com.joesem.sale.client.widget.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.widget.Button
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.google.android.material.tabs.TabLayout
import com.jiaxing.sale.client.R
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.utils.click
import java.text.SimpleDateFormat
import java.util.*




fun optionDatePickerRange(
    context: Context,
    selectedResult: (startDate: String, endDate: String, isReset: Boolean) -> Unit
) {
    @SuppressLint("SimpleDateFormat")
    val format = SimpleDateFormat("yyyy-MM-dd")
    var pickerView: TimePickerView? = null
    var startTime: Long = -1
    var endTime: Long = -1

    pickerView = TimePickerBuilder(
        context
    ) { date, v ->
        format.format(date).let {

        }
    }.setType(booleanArrayOf(true, true, true, false, false, false))
        .setContentTextSize(18)
        .isDialog(true).apply {
            setLayoutRes(
                R.layout.dialog_date_range
            ) { it ->
                val tab = it.findViewById<TabLayout>(R.id.tab_layout)
                val btnReset = it.findViewById<Button>(R.id.btn_reset)
                val btnConfirm = it.findViewById<Button>(R.id.btn_confirm)
                if (tab.selectedTabPosition == 0) {
                    startTime = System.currentTimeMillis()
                    tab.getTabAt(0)?.text = "起始日期\n${format.format(startTime)}"
                }
                btnReset.click {
                    selectedResult.invoke("", "", true)
                    pickerView?.dismiss()
                }
                btnConfirm.click {
                    if (startTime < 0) {
                        tab.selectTab(tab.getTabAt(0))
                        toast("请选择开始时间~")
                        return@click
                    }
                    if (endTime < 0) {
                        toast("请选择结束时间~")
                        tab.selectTab(tab.getTabAt(1))
                        return@click
                    }
                    if (startTime >= endTime) {
                        toast("开始时间那必须小于结束时间~")
                        tab.selectTab(tab.getTabAt(1))
                        return@click
                    }
                    selectedResult.invoke(
                        format.format(startTime),
                        format.format(endTime),
                        false
                    )
                    pickerView?.dismiss()
                }
                setTimeSelectChangeListener {
                    if (tab.selectedTabPosition == 0) {
                        startTime = it.time
                        tab.getTabAt(0)?.text = "起始日期\n${format.format(it)}"
                    } else {
                        endTime = it.time
                        tab.getTabAt(1)?.text = "截止日期\n${format.format(it)}"
                    }
                    Log.i("打印", "选择: ${format.format(it)}")
                }
            }
        }.build()
    pickerView?.show()
}

fun optionDatePickerSingle(
    context: Context,
    selectedResult: (data: String) -> Unit
) {
    @SuppressLint("SimpleDateFormat")
    val format = SimpleDateFormat("yyyy-MM-dd")
    var pickerView: TimePickerView? = null
    var selectedData = format.format(System.currentTimeMillis())

    pickerView = TimePickerBuilder(
        context
    ) { date, v ->
        format.format(date).let {

        }
    }.setType(booleanArrayOf(true, true, true, false, false, false))
        .setContentTextSize(18)
        .isDialog(true).apply {
            setLayoutRes(
                R.layout.dialog_date_single
            ) { it ->
                val btnCancel = it.findViewById<Button>(R.id.btn_cancel)
                val btnConfirm = it.findViewById<Button>(R.id.btn_confirm)
                btnCancel.click {
                    pickerView?.dismiss()
                }
                btnConfirm.click {
                    selectedResult.invoke(selectedData)
                    pickerView?.dismiss()
                }
                setTimeSelectChangeListener {
                    selectedData = format.format(it)

                }
            }
        }.build()
    pickerView?.show()
}